package com.hk.service.impl;

import org.apache.commons.io.filefilter.FalseFileFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hk.commons.Constant;
import com.hk.commons.ConstantPointRule;
import com.hk.commons.DateUtil;
import com.hk.commons.entity.Points;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.vo.app.CheckResultMsg;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.IPointsHistory;
import com.hk.service.IUserExtraInfo;
import com.hk.service.kyc.IPoints;
import com.hk.service.sys.ISysUser;

@Service
public class PointsImpl extends BaseServiceImpl<Points> implements IPoints {
	
	private Logger logger = LoggerFactory.getLogger(PointsImpl.class);
	
	@Autowired
	ISysUser userService;
	
	@Autowired
	IPointsHistory pointsHistoryService;
	
	
	@Autowired
	private IUserExtraInfo userExtraService;
	

	@Override
	public Points findById(Integer id) {
		return getById(id);
	}

	@Override
	public void add(Points model) {
		model.setCts(System.currentTimeMillis());
		super.save(model);
	}

	public Points delete(Integer id) {
		Points model = new Points();
		model.setId(id);
		delete(model);
		return model;
	}

	@Override
	public Points getPoinitsByType(Integer pointType) {
		return findById(pointType);
	}

	@Override
	public Integer getVal(Integer pointType) {
		logger.info("PointsImpl getVal pointType = " + pointType );
		Points points = findById(pointType);
		Integer addValue = 0;
		if (points != null) {
			addValue = points.getScore();
			if (points.getScoreType() == 0) {
				addValue = -1 * addValue;
			}
		}

		return addValue;
	}

	@Override
	public String getAddTieCardMsg(Long userId) {
		SysUser user = userService.findById(userId);
		String msg = "";
		if (user != null) {
			msg = "新用户进行绑卡, 用户手机号为" + user.getName() + " 时间:"
					+ DateUtil.longToLongDate(System.currentTimeMillis());
		} 
		return msg;
	}

	@Override
	public String getAddBespeakOrderMsg(Long bespeakOrderId) {

		String msg = "";
		if (bespeakOrderId != null) {
			msg = "用户预约, 预约订单编码为" + bespeakOrderId.toString() + " 时间:"
					+ DateUtil.longToLongDate(System.currentTimeMillis());
		}
		return msg;
	}

	@Override
	public String getAddPayOrderMsg(Long orderId) {
		String msg = "";
		if (orderId != null) {
			msg = "用户支付下单, 订单编码为" + orderId.toString() + " 时间:"
					+ DateUtil.longToLongDate(System.currentTimeMillis());
		}
		return msg;
	}

	@Override
	public String getAddUserMsg(Long userId) {
		SysUser user = userService.findById(userId);
		String msg = "";
		if (user != null) {
			msg = "新用户注册, 用户手机号为" + user.getName() + " 时间:"
					+ DateUtil.longToLongDate(System.currentTimeMillis());
		}
		return msg;
	}
	
	@Override
	public CheckResultMsg checkActive(Integer pointType, Long userId){
		
		CheckResultMsg checkResult = new CheckResultMsg();
		checkResult.setCheckResult(true);
		if(pointType != null){
			Points points = findById(pointType);
			if(points != null && points.getStatus() != null && points.getStatus() == Constant.ACTIVITY_ACTIVE ){
				Integer maxTimes = points.getMaxDayTimes();
				Integer times = pointsHistoryService.getDayTimes(pointType, userId);
				logger.info("checkActive maxTimes = " + maxTimes +  " times = " + times);
				if( times >= maxTimes){
					checkResult.setCheckResult(false);
					checkResult.setCheckResultCode(ConstantPointRule.POINT_RULE_ERROR03 );
					checkResult.setCheckResultMsg("已超过当日最大次数");
				}
			}else{
				checkResult.setCheckResult(false);
				checkResult.setCheckResultCode(ConstantPointRule.POINT_RULE_ERROR02);
				checkResult.setCheckResultMsg("积分活动尚未开始");
				logger.info("checkActive points != null && points.getStatus() == Constant.ACTIVITY_ACTIVE failed "
						+ " pointType = " + pointType +  "  userId = " + userId );
			}
		}else{
			checkResult.setCheckResult(false);
			checkResult.setCheckResultCode(ConstantPointRule.POINT_RULE_ERROR01);
			checkResult.setCheckResultMsg("积分活动已过期");
		}
		
		logger.info("check result = " + checkResult.getCheckResult());
		
		return checkResult;
	}
	
	@Override
	public CheckResultMsg checkPoints(Integer pointId, Long userId){
		CheckResultMsg resp = new CheckResultMsg();

		resp.setCheckResult(true);
		resp.setCheckResultCode(ConstantPointRule.POINT_RULE_NO_ERROR);
		
		Points points = findById(pointId);
		//01积分规则不存在
		if(points == null){
			//积分规则不存在
			resp.setCheckResult(false);
			resp.setCheckResultCode(ConstantPointRule.POINT_RULE_ERROR01);
			resp.setCheckResultMsg("积分活动已过期");
			 logger.info("points is null! pointsId = " + pointId);
			 return resp;
		}
		
		//03确定积分是否够用
		Integer leftPoints = userExtraService.getLeftPointsByUserId(userId);
		logger.info("left points = " + leftPoints);
		if(points.getScoreType() == ConstantPointRule.POINT_RULE_CUT && leftPoints < points.getScore() ){
			resp.setCheckResult(false);
			resp.setCheckResultCode(ConstantPointRule.POINT_RULE_ERROR04);
			resp.setCheckResultMsg("用户积分不足");
			logger.info("checkPointsEnough pointType = " + pointId + "  userId = " + userId
					+ " leftPoints =  " + leftPoints + "  need Points = " + points.getScore());
			return resp;
		}
		
		//04确定积分活动是否开始
		resp = checkActive(pointId, userId);
		 
		return resp;
	}

	@Override
	public Boolean checkExistById(Integer pointId){
		Points points = findById(pointId);
		return points != null?true:false;
	}

}
